package com.tandashi.springbootstudy01.service.impl;

import com.tandashi.springbootstudy01.dao.master.SourceDataTmpDao;
import com.tandashi.springbootstudy01.entity.master.SourceDataTmp;
import com.tandashi.springbootstudy01.service.SourceDataTmpService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * (SourceDataTmp)表服务实现类
 *
 * @author makejava
 * @since 2023-04-08 21:49:03
 */
@Service
public class SourceDataTmpServiceImpl implements SourceDataTmpService {
    @Resource
    private SourceDataTmpDao sourceDataTmpDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SourceDataTmp queryById(Long id) {
        return this.sourceDataTmpDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sourceDataTmp 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SourceDataTmp> queryByPage(SourceDataTmp sourceDataTmp, PageRequest pageRequest) {
        long total = this.sourceDataTmpDao.count(sourceDataTmp);
        return new PageImpl<>(this.sourceDataTmpDao.queryAllByLimit(sourceDataTmp, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sourceDataTmp 实例对象
     * @return 实例对象
     */
    @Override
    public SourceDataTmp insert(SourceDataTmp sourceDataTmp) {
        this.sourceDataTmpDao.insert(sourceDataTmp);
        return sourceDataTmp;
    }

    /**
     * 修改数据
     *
     * @param sourceDataTmp 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public SourceDataTmp update(SourceDataTmp sourceDataTmp) {
        this.sourceDataTmpDao.update(sourceDataTmp);
        //int i = 1/0;
        return this.queryById(sourceDataTmp.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sourceDataTmpDao.deleteById(id) > 0;
    }

    @Transactional
    public SourceDataTmp testA(Long id){
        SourceDataTmp tmp = this.queryById(id);
        tmp.setName("测试事务A");
        SourceDataTmp update = this.update(tmp);

        return update;
    }
}
